package com.keeson.android_cli.presenter;


import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

import com.keeson.android_cli.api.Api;
import com.keeson.android_cli.base.BasePresenterImpl;
import com.keeson.android_cli.base.BaseRespose;
import com.keeson.android_cli.base.retrofit.BaseObserver;
import com.keeson.android_cli.base.retrofit.RxSchedulers;
import com.keeson.android_cli.bean.EmptyObj;
import com.keeson.android_cli.interfaces.contract.LoginContract;
import com.keeson.android_cli.util.LogUtils;

/**
 * Created by jiaqiZHou
 * on 2020/12/4
 */
public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter {

    public LoginPresenter(LoginContract.View view) {
        super(view);
    }

    @Override
    public void getCode(String phone,String area_code) {
        view.showProgress("验证码获取中...");
        Api.getInstance().getCode(phone,area_code)
                .doOnSubscribe(disposable -> addDisposable(disposable))
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<BaseRespose<EmptyObj>>() {
                    @Override
                    protected void onSuccess(BaseRespose<EmptyObj> data) {
                        view.dismissProgress();
                        if (data == null) return;
                        if(data.isSuccess()){
                            view.getCodeSuccess(data.getData());
                        }else {
                            super.onError(data.getMsg(), data.getCode());
                        }
                    }
                    @Override
                    protected void onError(String msg, int errorCode) {
                        view.dismissProgress();
                        //view.getCodeFailed(errorCode,msg);
                        super.onError(msg, errorCode);
                    }
                });
    }


    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        LogUtils.e("生命周期====","onCreate");
    }

    @Override
    public void onResume(@NonNull LifecycleOwner owner) {
        LogUtils.e("生命周期====","onResume");
    }

    @Override
    public void onPause(@NonNull LifecycleOwner owner) {
        LogUtils.e("生命周期====","onPause");
    }

    @Override
    public void onStop(@NonNull LifecycleOwner owner) {
        LogUtils.e("生命周期====","onStop");
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        LogUtils.e("生命周期====","onDestroy");
    }
}